package com.ling.uiLogic
{
	import flash.events.MouseEvent;
	
	import mx.collections.XMLListCollection;
	import mx.core.UIComponent;
	
	import spark.components.DropDownList;
	import spark.components.Group;
	import spark.components.RadioButton;
	import spark.events.IndexChangeEvent;

	public class MapCategoryControl
	{
		private var mapParamPanel:Group;
		private var _root:UIComponent;
		
		private var normalRB:RadioButton;
		private var timeRB:RadioButton;
		private var currentRB:RadioButton;
		
		private var levelPropUI:IMapCategoryPropertyPanel;
		
		private var listData:XMLList = <>
											<item label="Get Target Point"/>
											<item label="Save Fruits"/>
			</>
		private var dropList:DropDownList;
		
		private var targetParamPanel:ITargetParamPanel;
		
		public function MapCategoryControl(app:LingEditor)
		{
			_root = app.borderRoot;
			
			normalRB = app.normalRB;
			timeRB = app.timeRB;
			mapParamPanel = app.mapParamPanel;
			
			normalRB.addEventListener(MouseEvent.CLICK,onClickRB);
			timeRB.addEventListener(MouseEvent.CLICK,onClickRB);
			//
			dropList = app.targetTypeList;
			dropList.dataProvider = new XMLListCollection(listData);
			dropList.labelField = "@label";
			dropList.addEventListener(IndexChangeEvent.CHANGE,onIndexChange);
			onIndexChange(null);
			//
			initLevelPropUI(normalRB);
		}
		
		
		public function set enabled(b:Boolean):void
		{
			_root.enabled = b;
		}
		
		private function onClickRB(event:MouseEvent):void
		{
			var rb:RadioButton = event.currentTarget as RadioButton;
			initLevelPropUI(rb);
		}
		
		private function initLevelPropUI(rb:RadioButton):void
		{
			if(rb == currentRB)
				return;
			currentRB = rb;
			
			if(levelPropUI)
				levelPropUI.destroy();
			//
			if(rb == normalRB)
				levelPropUI = new NormalLevelCategoryPropertyPanel();
			else
				levelPropUI = new TimeLevelCategoryPropertyPanel();
			
			mapParamPanel.addElementAt(levelPropUI.sprite,7);
		}
		
		
		private function onIndexChange(event:IndexChangeEvent):void
		{
			if(event == null)
			{
				dropList.selectedIndex = 0;
				targetParamPanel = new PointTargetSetPanel();
				mapParamPanel.addElement(targetParamPanel.sprite);
				return;
			}
			var index:int = event.newIndex;
			if(targetParamPanel)
				targetParamPanel.destroy();
			
			if(index == 0)
				targetParamPanel = new PointTargetSetPanel();
			else
				targetParamPanel = new SaveOtherTargetSetPanel();
			
			mapParamPanel.addElement(targetParamPanel.sprite);
		}
		
		
		public function getXML():XML
		{
			var xml:XML = <mapCategory/>;
			if(currentRB == normalRB)
				xml.@category = "0";
			else
				xml.@category = "1";
			
			xml.appendChild(levelPropUI.getXml());
			xml.appendChild(targetParamPanel.getXml());
			
			return xml;
		}
	}
}